package com.dy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.mapper.DyUserMapper;
import com.dy.pojo.dto.DyUserDTO;
import com.dy.pojo.dto.DyUserRestDTO;
import com.dy.pojo.entity.DyUserEntity;
import com.dy.service.DyUserService;
import com.dy.utils.ApiResousUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: UserController
 * @Date: 2021/6/1 17:32
 * @Version 1.0 (版本)
 */
@Controller
@RequestMapping("/user")
public class DyUserController {
    private final Logger log= LogManager.getLogger("DyUserController.class");
    @Autowired
    private DyUserService dyUserService;
    @Autowired
    private DyUserMapper dyUserMapper;
    /**
     * 登录
     * @param dyUserDTO
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public ApiResousUtil<String> loginUser(@RequestBody DyUserDTO dyUserDTO, HttpSession session){
        log.info("---执行到login---");
        try{
            dyUserService.loginUser(dyUserDTO);
            String code = dyUserService.listcode(dyUserDTO.getUsername());
            DyUserEntity selectOne = dyUserMapper.selectOne(new QueryWrapper<DyUserEntity>().eq("username", dyUserDTO.getUsername()));
            selectOne.setPassword(null);
            session.setAttribute("code",code);
            session.setAttribute("user",selectOne);
        }catch (Exception e){
            if (e instanceof UnknownAccountException) {
                return ApiResousUtil.error("登录失败，用户名不存在！");
            }else if (e instanceof IncorrectCredentialsException) {
                return ApiResousUtil.error("登录失败，用户密码不正确！");
            }else {
                return ApiResousUtil.error("登录失败，未知错误！");
            }
        }
        return ApiResousUtil.ok("登录成功！");
    }

    /**
     * 注册
     * @param dyUserRestDTO
     * @return ApiResousUtil
     */
    @RequiresPermissions("u_blog_save")
    @PostMapping("/restUser")
    @ResponseBody
    public ApiResousUtil<String> restUser(@RequestBody DyUserRestDTO dyUserRestDTO){
        if (dyUserRestDTO == null || dyUserRestDTO.getNickname() == null || dyUserRestDTO.getUsername() == null
                || dyUserRestDTO.getPassword()== null || dyUserRestDTO.getEmail() ==null
                || dyUserRestDTO.getAvatar()==null)
        {
            return ApiResousUtil.error("注册失败，参数不能为空！");
        }
        boolean f = dyUserService.restUser(dyUserRestDTO);
        if (f == false) {
            return ApiResousUtil.error("注册失败，已经有此用户名！");
        }
        return ApiResousUtil.ok();
    }
    /**
     * 注销
     * @param session
     * @return ApiResousUtil
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        try{
            session.getAttribute("user");
            session.invalidate();
        }catch (Exception e){

        }
        return "admin/login";
    }

}
